package com.dycloud.project.event;


import cn.hutool.core.util.StrUtil;
import com.dycloud.project.constant.Constants;
import com.dycloud.project.domain.UserLikeRecord;
import com.dycloud.project.enums.LikeTargetTypeEnum;
import com.dycloud.project.service.ICourseService;
import com.dycloud.project.service.IIndustryTrendsService;
import com.dycloud.project.service.IUserLikeRecordService;
import com.dycloud.project.vo.UserCourseVO;
import com.dycloud.project.vo.UserIndustryTrendsVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author : renhang
 * @description LikeEventListener
 * @date : 2025-07-02 23:12
 **/
@Component
@Slf4j
public class LikeEventListener {

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private IUserLikeRecordService userLikeRecordService;


    @EventListener
    public void likeEvent(LikeEvent likeEvent) {
        String classNameByTargetType = LikeTargetTypeEnum.getClassNameByTargetType(likeEvent.getTargetType());
        if (StrUtil.isBlank(classNameByTargetType)) {
            log.error("likeEvent Error! classNameByTargetType is null, targetType is {}", likeEvent.getTargetType());
            return;
        }

        UserLikeRecord record = UserLikeRecord.builder().targetType(likeEvent.getTargetType())
                .targetId(likeEvent.getId())
                .userId(likeEvent.getUserId())
                .userName(likeEvent.getUsername()).build();

        Object bean = applicationContext.getBean(classNameByTargetType);
        if (bean instanceof IIndustryTrendsService iIndustryTrendsService) {
            // 行业动态
            UserIndustryTrendsVO industryTrends = iIndustryTrendsService.selectIndustryTrendsById(likeEvent.getId());
            record.setTargetName(industryTrends.getTitle());
            record.setCoverImage(industryTrends.getCoverImage());
            record.setSummary(industryTrends.getSummary());
        }
        if (bean instanceof ICourseService iCourseService) {
            // 课程
            UserCourseVO course = iCourseService.selectCourseById(likeEvent.getId());
            record.setTargetName(course.getName());
            record.setCoverImage(course.getCoverImage());
            record.setSummary(course.getDescription());
        }


        if (Objects.equals(likeEvent.getLikeStatus(), Constants.ONE)) {
            // 先要根据 类型+userId+id 查询，如果点赞过了，不能再点赞
            UserLikeRecord userLikeRecord = userLikeRecordService.queryUserIsLiked(likeEvent.getTargetType(), likeEvent.getId(), likeEvent.getUserId());
            if (Objects.nonNull(userLikeRecord)) {
                log.error("您已经点过赞了！");
                return;
            }
            userLikeRecordService.insertUserLikeRecord(record);
        } else {
            //  取消点赞， type+userId+id 删除一条点赞记录
            userLikeRecordService.deleteUserLikeRecord(likeEvent.getTargetType(), likeEvent.getUserId(), likeEvent.getId());
        }

    }
}
